package main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;

import generator.SchemeGenerator;
import model.Annotation;
import model.Choice;
import model.EnclosingType;
import model.Not;
import model.Sibling;
import model.Twin;

public class Main {

	public static void main(String[] args) {

		//Create annotation constraint model===================
		Annotation annotation = new Annotation("A");
//		annotation.setMinOccurs(4);
//		annotation.setMaxOccurs(2);
//		annotation.setMinParamCount(1);
//		annotation.setMaxParamCount(2);
//		annotation.setParamType("T");
//		annotation.setReturnType("V");
//		annotation.setFieldType("T");
//		annotation.setPrefix("Main");
		
//		EnclosingType enclosingType = new EnclosingType();
//		enclosingType.addChild(new Annotation("EA"));
//		enclosingType.addChild(new Not(new Annotation("NEA")));
//		enclosingType.addChild(new Choice(new Annotation("EB"), new Annotation("EC")));
//		annotation.setEnclosingType(enclosingType);
		
//		Sibling siblings = new Sibling();
//		siblings.addSibling(new Annotation("C"));
//		siblings.addSibling(new Not(new Annotation("A")));
//		siblings.addSibling(new Choice(new Annotation("D"), new Annotation("C")));
//		annotation.setSiblings(siblings);
		
//		Twin twins = new Twin();
//		twins.addTwin(new Annotation("C"));
//		twins.addTwin(new Not(new Annotation("A")));
//		twins.addTwin(new Choice(new Annotation("D"), new Annotation("C")));
//		annotation.setTwins(twins);
				
		//Create schematron scheme==============================
		SchemeGenerator schemeGenerator = new SchemeGenerator();
		String scheme = schemeGenerator.generateXmlSchema(annotation);
		System.out.println(scheme);
		//System.out.println(System.getProperty("user.dir"));
		
		//Save to XML file=========================================
		new File("input").mkdir();
		File file = new File("generator/input/schema.xml");
	    Writer output;
		try {
			output = new BufferedWriter(new FileWriter(file));
			output.write(scheme);
			output.close();
	    } catch (IOException e) {
			e.printStackTrace();
		}
		
		//Validate via probatron
		File inputModel = new File("generator/input/model.xml");
		File inputScheme = new File("generator/input/schema.xml");
		File outputModel = new File("generator/output/validation.xml");
		File probatronJar = new File("generator/probatron.jar");
		
		
		Runtime rt = Runtime.getRuntime();
		try {
			rt.exec("cmd /C java -jar "+probatronJar.getPath()+" "+inputModel.getPath()+" "+inputScheme.getPath()+" > "+outputModel.getPath());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
